package com.casic.ops.entity.vo;


import com.casic.ops.consts.app.PipelineDetailStatus;
import com.casic.ops.consts.app.StageType;
import com.casic.ops.entity.domain.ApplicationPipelineTaskDetailDO;
import com.casic.ops.utils.Utils;
import com.orion.utils.convert.TypeStore;
import lombok.Data;

import java.util.Date;

/**
 * @ClassName ops-platform
 * @Description  应用流水线统计分析操作日志
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Data
public class ApplicationPipelineTaskStatisticsDetailVO {

    /**
     * id
     */
    private Long id;

    /**
     * 操作id
     */
    private Long detailId;

    /**
     * 引用id
     */
    private Long relId;

    /**
     * 阶段类型 10构建 20发布
     *
     * @see StageType
     */
    private Integer stageType;

    /**
     * 状态 10未开始 20进行中 30已完成 40执行失败 50已跳过 60已终止
     *
     * @see PipelineDetailStatus
     */
    private Integer status;

    /**
     * 操作时长 (任何状态)
     */
    private Long used;

    /**
     * 操作时长 (任何状态)
     */
    private String usedInterval;

    public static ApplicationPipelineTaskStatisticsDetailVO ApipelineTaskDetailDO2pipelineTaskStatisticsDetailVO(ApplicationPipelineTaskDetailDO p) {
        ApplicationPipelineTaskStatisticsDetailVO vo = new ApplicationPipelineTaskStatisticsDetailVO();
        vo.setId(p.getId());
        vo.setRelId(p.getRelId());
        vo.setStageType(p.getStageType());
        vo.setStatus(p.getExecStatus());
        Date startTime = p.getExecStartTime();
        Date endTime = p.getExecEndTime();
        if (startTime != null && endTime != null) {
            long used = endTime.getTime() - startTime.getTime();
            vo.setUsed(used);
            vo.setUsedInterval(Utils.interval(used));
        }
        return vo;
    }

    static {
        TypeStore.STORE.register(ApplicationPipelineTaskDetailDO.class, ApplicationPipelineTaskStatisticsDetailVO.class, p -> {
            ApplicationPipelineTaskStatisticsDetailVO vo = new ApplicationPipelineTaskStatisticsDetailVO();
            vo.setId(p.getId());
            vo.setRelId(p.getRelId());
            vo.setStageType(p.getStageType());
            vo.setStatus(p.getExecStatus());
            Date startTime = p.getExecStartTime();
            Date endTime = p.getExecEndTime();
            if (startTime != null && endTime != null) {
                long used = endTime.getTime() - startTime.getTime();
                vo.setUsed(used);
                vo.setUsedInterval(Utils.interval(used));
            }
            return vo;
        });
    }
}
